import { defineStore } from "pinia";
import NoticeAPI from "@/api/system/notice";
import { NoticePageVO } from "@/api/system/notice";

export const useNoticeStore = defineStore("notice", {
  state: () => ({
    notices: [] as NoticePageVO[],
  }),

  actions: {
    async fetchNotices() {
      const data = await NoticeAPI.getMyNoticePage({ pageNum: 1, pageSize: 5, isRead: 0 });
      this.notices = data.list;
    },

    addNotice(notice: NoticePageVO) {
      if (!this.notices.some((item) => item.id === notice.id)) {
        this.notices.unshift(notice);
      }
    },

    removeNotice(id: string) {
      const index = this.notices.findIndex((notice) => notice.id === id);
      if (index > -1) {
        this.notices.splice(index, 1);
      }
    },

    clearNotices() {
      this.notices = [];
    },
  },
});
